
package com.producttypemodule.dao;
import com.producttypemodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbProductTypeDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbProductType tbProductType=null;
    private List<TbProductType> tbProductTypeList=null;

    public boolean add(TbProductType inTbProductType){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_ProductType(ProductTypeLevel,ProductTypeName,ParentTypeId,IsMaxProductType,ProductTypeMemo) values(?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbProductType.getProductTypeLevel());
        pt.setString(2,inTbProductType.getProductTypeName());
        pt.setInt(3,inTbProductType.getParentTypeId());
        pt.setInt(4,inTbProductType.getIsMaxProductType());
        pt.setString(5,inTbProductType.getProductTypeMemo());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inProductTypeId){
        return UtilDAO.del("Tb_ProductType", "ProductTypeId", inProductTypeId);
    }
    public boolean edit(TbProductType inTbProductType){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_ProductType set  ProductTypeLevel=?,ProductTypeName=?,ParentTypeId=?,IsMaxProductType=?,ProductTypeMemo=? where ProductTypeId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbProductType.getProductTypeLevel());
        pt.setString(2,inTbProductType.getProductTypeName());
        pt.setInt(3,inTbProductType.getParentTypeId());
        pt.setInt(4,inTbProductType.getIsMaxProductType());
        pt.setString(5,inTbProductType.getProductTypeMemo());
        pt.setInt(6,inTbProductType.getProductTypeId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbProductType getTbProductTypeByProductTypeId(int inProductTypeId) {
        tbProductType=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductType where ProductTypeId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inProductTypeId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbProductType == null)tbProductType = getTbProductType(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProductType;
    }
    public List<TbProductType> getTbProductTypeByParentTypeId(int inProductTypeId) {
    	 tbProductTypeList=new ArrayList<TbProductType>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductType where ParentTypeId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inProductTypeId);
            System.out.println("!!!!"+sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductTypeList.add(getTbProductType(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProductTypeList;
    }
    public List<TbProductType> getTbProductTypeAll() {
        tbProductTypeList=new ArrayList<TbProductType>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductType";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductTypeList.add(getTbProductType(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProductTypeList;
    }
    public List<TbProductType> getTbProductTypeAll(VskPage vp) {
        tbProductTypeList=new ArrayList<TbProductType>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_ProductType";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_ProductType", "ProductTypeId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProductTypeList.add(getTbProductType(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProductTypeList;
    }
    public TbProductType getTbProductType(ResultSet rs) throws Exception{
        TbProductType tbProductType = new TbProductType();
        tbProductType.setProductTypeId(rs.getInt("ProductTypeId"));
        tbProductType.setProductTypeLevel(rs.getInt("ProductTypeLevel"));
        tbProductType.setProductTypeName(rs.getString("ProductTypeName"));
        tbProductType.setParentTypeId(rs.getInt("ParentTypeId"));
        tbProductType.setIsMaxProductType(rs.getInt("IsMaxProductType"));
        tbProductType.setProductTypeMemo(rs.getString("ProductTypeMemo"));
        tbProductType.setCreateTime(rs.getString("CreateTime"));
        return tbProductType;
    }
}
